import { _decorator, Component, Event, Label, Node, tween, Vec3 } from 'cc';
import { EasingType } from 'db://assets/Scripts/Framework/Utils/TweenUtil';
import { GameDataManager } from '../GameDataManager';
import { MainUI } from './MainUI';
import { ProjectEventManager, ProjectEvent } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('TaskUI')
export class TaskUI extends Component {

    @property(Node)
    layout: Node;

    maxRequire = [1, 10, 3];
    tokens = [500, 500, 100];

    protected onEnable(): void {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        this._Init();
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    _Init() {
        let data = GameDataManager.Instance.getArrayData("Tasks");
        let data2 = GameDataManager.Instance.getArrayData("TaskRequires");
        for (let i = 0; i < this.layout.children.length; i++) {
            const element = this.layout.children[i];
            element.children[2].getComponent(Label).string = `${data2[i]}/${this.maxRequire[i]}`;
            let mask = element.children[3].children[1];
            mask.active = data[i] == 1 || data2[i] < this.maxRequire[i];
            mask.children[0].getComponent(Label).string = data[i] == 1 ? "已领取" : "未完成";
        }
    }

    Back() {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ZERO }, { easing: EasingType.backIn })
            .call(() => { this.node.active = false; })
            .start();
        MainUI.Instance.PlayClick();
    }

    Finish(event: Event) {
        let target: Node = event.target;
        let num = target.parent.parent.getSiblingIndex();
        let data = GameDataManager.Instance.getArrayData("Tasks");
        data[num] = 1;
        GameDataManager.Instance.setArrayData("Tasks", data);
        let delta = this.tokens[num];
        let label = MainUI.Instance.finish.children[3].getComponent(Label);
        if (num < 2) {
            let coin = GameDataManager.Instance.getNumberData("Coin");
            coin += delta;
            GameDataManager.Instance.setNumberData("Coin", coin);
            label.string = `金币x${delta}`;
        }
        else {
            let diamond = GameDataManager.Instance.getNumberData("Diamond");
            diamond += delta;
            GameDataManager.Instance.setNumberData("Diamond", diamond);
            label.string = `钻石x${delta}`;
        }
        MainUI.Instance._InitToken();
        this._Init();
        for (let i = 0; i < MainUI.Instance.finish.children[2].children.length; i++) {
            const element = MainUI.Instance.finish.children[2].children[i];
            element.active = i == 0 && num < 2 || i == 1 && num == 2;
        }
        tween(MainUI.Instance.finish)
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        MainUI.Instance.PlayClick();
    }

    FinishBtn() {
        tween(MainUI.Instance.finish)
            .to(0.5, { scale: Vec3.ZERO }, { easing: EasingType.backIn })
            .start();
        MainUI.Instance.PlayBonus();
    }
}


